﻿using System;
using System.Text;

namespace buildtool
{
    public class ProjConfig
    {
        public void Save(string filename)
        {
            if (System.IO.File.Exists(filename))
                System.IO.File.Delete(filename);
            Newtonsoft.Json.Linq.JObject obj = new Newtonsoft.Json.Linq.JObject();
            var outstr = obj.ToString(Newtonsoft.Json.Formatting.Indented);
            System.IO.File.WriteAllText(filename, outstr);
        }
    }
    public static class BuildTool
    {
        public static bool CreateProj(string path)
        {
            if (HaveProj(path))
                return false;
            if (System.IO.Directory.Exists(path) == false)
            {
                System.IO.Directory.CreateDirectory(path);
            }
            System.IO.File.Copy("sample\\background.asm", path + "\\background.asm", true);
            System.IO.File.Copy("sample\\mario.chr", path + "\\mario.chr", true);
            ProjConfig config = new ProjConfig();
            config.Save(path + "\\nes.config");
            return true;

        }
        public static bool OpenProj(string path)
        {
            if (HaveProj(path) == false)
                return false;
            return true;
        }
        public static bool HaveProj(string path)
        {
            string config = System.IO.Path.Combine(path, "nes.config");
            return System.IO.File.Exists(config);

        }
        static string cmdfile
        {
            get
            {
                bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
                return isWindows ? "cmd" : "bash";
            }
        }
        static string electronfile
        {
            get
            {
                bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
                return isWindows ? "electron.cmd" : "./electron";

            }
        }


        static void runCommand(string path, string param, out string output, out string errinfo)
        {
            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
            info.FileName = "3rd\\nesasm3.exe";
            info.WorkingDirectory = path;
            info.Arguments = param;
            info.RedirectStandardOutput = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;
            System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);

            //p.StandardInput.WriteLine(cmd);
            //p.StandardInput.WriteLine("exit");
            p.WaitForExit();

            output = p.StandardOutput.ReadToEnd();
            errinfo = p.StandardError.ReadToEnd();
        }
        public static byte[] BuildProj(string proj, out string outpstr)
        {
            outpstr = null;
            if (!HaveProj(proj))
                return null;
            var inputfile = "background.asm";
            var outputfile = System.IO.Path.Combine(proj, "background.nes");
            System.IO.File.Delete(outputfile);
            //runCommand(proj, inputfile +" -l3", out  outpstr, out string errstr);
            Build(proj, inputfile, outputfile, out outpstr);
            if (System.IO.File.Exists(outputfile))
            {
                var image = System.IO.File.ReadAllBytes(outputfile);
                System.IO.File.Delete(outputfile);
                return image;
            }
            return null;
        }
        static void Build(string proj, string inputfile, string outputfile, out string outputstr)
        {
            var curpath = System.IO.Directory.GetCurrentDirectory();
            try
            {
                System.IO.Directory.SetCurrentDirectory(proj);
                StringBuilder sb = new StringBuilder();
                
                var opt =   new NesAsmSharp.Assembler.NesAsmOption
                {
                    ListLevel = 2,
                    HeaderOpt = true,
                    DeveloOpt = false,
                    MListOpt = false,
                    SrecOpt = false,
                    RunOpt = 0,
                    ScdOpt = false,
                    CdOpt = false,
                    MxOpt = false,
                    AutoZPOpt = false
                };
                opt.InFName = inputfile;
                opt.OutFName = outputfile;
                var assem = new NesAsmSharp.Assembler.NesAssembler(opt);
                assem.Assemble();
                outputstr = opt.StdOut.ToString();
            }
            catch (Exception err)
            {
                outputstr = err.Message;
            }
            System.IO.Directory.SetCurrentDirectory(curpath);

        }
    }
}
